package com.shms.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfClass;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShClass;
import com.shms.service.ShClassSevice;

public class ClassAction  extends ActionSupport{
	private static final long serialVersionUID = -7106008904232245508L;
	private ShClassSevice shClassService;
	private GroupOfClass groupOfClassess;
	public ShClass shClass;
	private Integer[] nos;
	public String message;

	public String addClass() {
		//
		ActionContext.getContext().put("gradename", 0);
		return SUCCESS;
	}
	
	public String queryByClassId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShClass shClassParams = new ShClass();
		shClassParams.setCid(id);
		shClass = shClassService.queryById(shClassParams);
		//
		ActionContext.getContext().put("gradename", shClass.getGid());
		return SUCCESS;
	}
	
	/**
	 * 根据分页信息，查询宿舍楼列表
	 * @return 
	 */
	public String queryClass() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询动态信息的分页信息输入参数
		GroupOfClass ClassOfParams = new GroupOfClass();
		ClassOfParams.setCurrentPage(currentPage);
		ClassOfParams.setType(type);
		ClassOfParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfClassess = shClassService.queryClass(ClassOfParams);
		if (groupOfClassess.getClass() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	/**
	 * 批量删除宿舍楼信息
	 * @return 
	 */
	public String deleteClasses() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			ShClass sc=new ShClass();
			sc.setCid(Long.parseLong(ServletActionContext.getRequest().getParameter("no")));
			if(shClassService.queryStudentsById(sc).size()>0){
				message="该班级下已经存在学生记录，不允许删除";
				//获取传来的当前页数
				Integer currentPage =groupOfClassess.getCurrentPage()-1;
				//获取传来的上一页或下一页标志
				String type = groupOfClassess.getType();
				//构造查询动态信息的分页信息输入参数
				GroupOfClass groupOfClassesParams = new GroupOfClass();
				groupOfClassesParams.setCurrentPage(currentPage);
				groupOfClassesParams.setType(type);
				groupOfClassesParams.setPageSize(QueryConstant.F_Pagesize);
				//查询
				groupOfClassess = shClassService.queryClass(groupOfClassesParams);
				return INPUT;
			}
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shClassService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage =groupOfClassess.getCurrentPage()-1;
			//获取传来的上一页或下一页标志
			String type = groupOfClassess.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfClass groupOfClassesParams = new GroupOfClass();
			groupOfClassesParams.setCurrentPage(currentPage);
			groupOfClassesParams.setType(type);
			groupOfClassesParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfClassess = shClassService.queryClass(groupOfClassesParams);
			return SUCCESS;
		}else{
			message = "删除宿舍楼信息失败";
			return INPUT;
		}
	}
	/**
	 * 保存班级详细信息
	 * @return
	 */
	public String saveClasses() {
		boolean rtn = shClassService.saveClasses(shClass);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		ActionContext.getContext().put("gradename", shClass.getGid());
		return SUCCESS;
	}
	public ShClass getShClass() {
		return shClass;
	}
	public void setShClass(ShClass shClass) {
		this.shClass = shClass;
	}
	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public GroupOfClass getGroupOfClassess() {
		return groupOfClassess;
	}
	public void setGroupOfClassess(GroupOfClass groupOfClassess) {
		this.groupOfClassess = groupOfClassess;
	}
	public void setShClassService(ShClassSevice shClassService) {
		this.shClassService = shClassService;
	}
}
